package com.ledim.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.RoomPlayVideoBean;
import com.ledim.itf.LedimOnItemClickListener;
import com.ledim.utils.ConstantUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RoomDragListViewAdapter extends LedimBaseListAdapter<RoomPlayVideoBean> {

    private LayoutInflater mInflater;
    public SparseBooleanArray mSelectionMap;
    public ArrayList<RoomPlayVideoBean> mSelectedEpisodes;
    private LedimOnItemClickListener<List<RoomPlayVideoBean>> mListener;

    public RoomDragListViewAdapter(Context context,
                                   LedimOnItemClickListener<List<RoomPlayVideoBean>> listener,
                                   ArrayList<RoomPlayVideoBean> list) {
        super(context);

        mInflater = LayoutInflater.from(context);
        mSelectionMap = new SparseBooleanArray();
        mSelectedEpisodes = new ArrayList<RoomPlayVideoBean>();
        this.mListener = listener;
        setDatas(list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        //不要复用  以免拖拽listviewItem时拖动的阴影有不同步
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_room_playlist_drag_listview, null);
            holder.mName = (TextView) convertView.findViewById(R.id.item_drag_play_name);
            holder.mState = (TextView) convertView.findViewById(R.id.item_drag_play_state);
            holder.mPlayMark = (ImageView) convertView.findViewById(R.id.item_drag_play_mark);
            holder.mDragMark = (ImageView) convertView.findViewById(R.id.item_drag_drag_mark);
            holder.mCheckMArk = (ImageView) convertView.findViewById(R.id.item_drag_check);
            holder.mBg = convertView.findViewById(R.id.item_room_playlist_drag_bg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final RoomPlayVideoBean roomPlayVideoBean = getItem(position);

        if (roomPlayVideoBean.title != null && roomPlayVideoBean.title.length() > 0) {
            holder.mName.setText(roomPlayVideoBean.title);
        } else {
            holder.mName.setText("");
        }

        holder.mBg.setBackgroundColor(Color.parseColor("#ffffff"));
        if (getCount() == 1) {
            holder.mDragMark.setVisibility(View.GONE);
            holder.mCheckMArk.setVisibility(View.GONE);
        } else {
            if (roomPlayVideoBean.play_status == ConstantUtil.PlayState.wait) {
                holder.mDragMark.setVisibility(View.VISIBLE);
                holder.mCheckMArk.setVisibility(View.VISIBLE);
            } else {
                holder.mDragMark.setVisibility(View.GONE);
                holder.mCheckMArk.setVisibility(View.GONE);
            }
        }
        switch (roomPlayVideoBean.play_status) {
            case ConstantUtil.PlayState.palying:
                holder.mState.setText("放映中");
                holder.mState.setTextColor(Color.parseColor("#1a1a1a"));
                holder.mName.setTextColor(Color.parseColor("#1a1a1a"));
                holder.mPlayMark.setVisibility(View.VISIBLE);
                holder.mBg.setBackgroundColor(Color.parseColor("#FEFBBD"));
                break;

            case ConstantUtil.PlayState.end:
                holder.mPlayMark.setVisibility(View.GONE);
                holder.mState.setText("已结束");
                holder.mState.setTextColor(Color.parseColor("#999999"));
                holder.mName.setTextColor(Color.parseColor("#999999"));
                break;

            case ConstantUtil.PlayState.wait:
                holder.mState.setText("");
                holder.mCheckMArk.setImageResource(R.drawable.unchecked);
                holder.mName.setTextColor(Color.parseColor("#666666"));
                break;

            default:
                holder.mState.setText("即将放映");
                holder.mName.setTextColor(Color.parseColor("#1a1a1a"));
                holder.mState.setTextColor(Color.parseColor("#1a1a1a"));
                break;
        }

        listViewMulChoices(holder.mCheckMArk, position);
        return convertView;
    }

    public void getSelectEpisodes(ImageView mCheckMArk, int pos) {
        if (mSelectionMap.get(pos)) {
            mSelectionMap.put(pos, false);
            mCheckMArk.setImageResource(R.drawable.unchecked);
            Iterator<RoomPlayVideoBean> it = mSelectedEpisodes.iterator();
            while (it.hasNext()) {
                if (it.next() == getItem(pos)) {
                    it.remove();
                }
            }
        } else {
            mSelectionMap.put(pos, true);
            mCheckMArk.setImageResource(R.drawable.check);
            mSelectedEpisodes.add(getItem(pos));
        }
        if (mListener != null) {
            mListener.onItemClick(pos, mSelectedEpisodes, mCheckMArk);
        }

    }

    private void listViewMulChoices(ImageView mCheckMArk, int position) {
        if (mSelectionMap.get(position)) {
            mCheckMArk.setImageResource(R.drawable.check);
        } else {
            mCheckMArk.setImageResource(R.drawable.unchecked);
        }
    }

    public static class ViewHolder {

        private TextView mName;
        private TextView mState;
        private ImageView mPlayMark;
        private ImageView mDragMark;
        private ImageView mCheckMArk;
        private View mBg;
    }
}
